package org.monkey.common.constant.entity;

import lombok.Data;
import org.monkey.common.constant.config.CommonConstant;
import org.monkey.common.constant.exception.BaseException;

@Data
public class Result<T> {

  private int code = 200;
  private String msg = "success";
  private String devMsg = CommonConstant.EMPTY_STRING;
  private T data;
  private long ts = System.currentTimeMillis();

  private Result() {}

  public Result(T t) {
    if (t != null) {
      this.data = t;
    }
  }

  private Result(ErrorCode errorCode) {
    this.code = errorCode.getCode();
    this.msg = errorCode.getMsg();
  }

  private Result(ErrorCode errorCode, String devMsg) {
    this.code = errorCode.getCode();
    this.msg = errorCode.getMsg();
    this.devMsg = devMsg;
  }

  private Result(int errorCode, String msg, String devMsg) {
    this.code = errorCode;
    this.msg = msg;
    this.devMsg = devMsg;
  }

  public static <T> Result<T> success() {
    return new Result();
  }

  public static <T> Result<T> success(T t) {
    return new Result<T>(t);
  }

  public static <T> Result error(ErrorCode errorCode) {
    return new Result<T>(errorCode);
  }

  public static <T> Result error(ErrorCode errorCode, String devMsg) {
    return new Result<T>(errorCode, devMsg);
  }

  public static Result<String> error(BaseException baseException) {
    return new Result<String>(
        baseException.getStatus(), baseException.getMessage(), baseException.getDevMsg());
  }
}
